/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.controller;

import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import br.ufmg.dees.insane.commons.controller.Controller;
import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.draw.AxisDraw;
import br.ufmg.dees.insane.draw.DiagramDraw;
import br.ufmg.dees.insane.draw.Draw;
import br.ufmg.dees.insane.draw.LiberationDraw;
import br.ufmg.dees.insane.draw.LineDraw;
import br.ufmg.dees.insane.draw.PointDraw;
import br.ufmg.dees.insane.draw.SpringDraw;
import br.ufmg.dees.insane.draw.SupportDraw;
import br.ufmg.dees.insane.materialMedia.material.LinearElasticIsotropic;
import br.ufmg.dees.insane.materialMedia.material.MSpring;
import br.ufmg.dees.insane.model.Model;
import br.ufmg.dees.insane.model.disc.analysismodel.FrameElmAnalysis;
import br.ufmg.dees.insane.model.disc.element.Element;
import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.model.disc.pointforce.PointForce;
import br.ufmg.dees.insane.ui.rich.full.Effort;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;
import br.ufmg.dees.insane.util.IMatrix;
import br.ufmg.dees.insane.util.IPoint3d;

/**
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since July 2004
 */
public class DiagramsController implements Controller {
	
	protected InsaneInternalInterface parent;
	protected Model model;
	protected FemModel fmodel;
	protected DrawingArea da;
	protected ArrayList<Object> selection = new ArrayList<Object>();
	
	protected ArrayList<Draw> nodes = new ArrayList<Draw>();
	protected ArrayList<Draw> elements = new ArrayList<Draw>();
	
	protected ArrayList<Draw> supports = new ArrayList<Draw>();
	protected ArrayList<Draw> springs = new ArrayList<Draw>();
	
	protected ArrayList<Draw> localAxis = new ArrayList<Draw>();
	protected ArrayList<Draw> liberations = new ArrayList<Draw>();
	protected ArrayList<Draw> elasticConnections = new ArrayList<Draw>();
	
	protected ArrayList<Draw> fxDiagrams = new ArrayList<Draw>();
	protected ArrayList<Draw> fyDiagrams = new ArrayList<Draw>();
	protected ArrayList<Draw> fzDiagrams = new ArrayList<Draw>();
	protected ArrayList<Draw> mxDiagrams = new ArrayList<Draw>();
	protected ArrayList<Draw> myDiagrams = new ArrayList<Draw>();
	protected ArrayList<Draw> mzDiagrams = new ArrayList<Draw>();
	
	protected ArrayList<Draw> dxDiagrams = new ArrayList<Draw>();
	protected ArrayList<Draw> dyDiagrams = new ArrayList<Draw>();
	protected ArrayList<Draw> dzDiagrams = new ArrayList<Draw>();
	protected ArrayList<Draw> rxDiagrams = new ArrayList<Draw>();
	protected ArrayList<Draw> ryDiagrams = new ArrayList<Draw>();
	protected ArrayList<Draw> rzDiagrams = new ArrayList<Draw>();
	
	protected double sFx = 1;
	protected double sFy = 1;
	protected double sFz = 1;
	protected double sMx = 1;
	protected double sMy = 1;
	protected double sMz = 1;
	
	protected double sDx = 1;
	protected double sDy = 1;
	protected double sDz = 1;
	protected double sRx = 1;
	protected double sRy = 1;
	protected double sRz = 1;
	
//*****************************************************************************
	
    /** The constructor method. */
    public DiagramsController(Model model, DrawingArea da) {
		this.model = model;
        this.da = da;
        this.fmodel = this.model.getDiscreteModel();
    }
	
//*****************************************************************************
	
	public void setParent(InsaneInternalInterface par) {
		this.parent = par;
	}
	
//*****************************************************************************
	
    public void draw(Graphics2D g) {
        
		double nodeScale = model.getState().nodeScale;
		double supportScale = model.getState().supportScale;
		double diagramScale = model.getState().diagramScale;
		
        g.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
		
		Draw draw;
		Node no;
		
		//Draws global axis if selected
		if (model.getState().getViewGlobalAxis()) {
			g.setColor(model.getState().globalAxisColor);
			draw = new AxisDraw();
			((AxisDraw)draw).setLabel1("X");
			((AxisDraw)draw).setLabel2("Y");
			draw.setLocation(new Point(10, da.getHeight()-10));
			draw.draw(g);
		}
		
		//Draws elements
		ListIterator lns = elements.listIterator();
		while(lns.hasNext()) {
			g.setColor(model.getState().lineColor);
			draw = (Draw)lns.next();
			draw.draw(g);
		}
		
		//Draws local axis
		if (model.getState().getViewLocalAxis()) {
			ListIterator la = localAxis.listIterator();
			while(la.hasNext()) {
				g.setColor(model.getState().localAxisColor);
				draw = (Draw)la.next();
				draw.draw(g);
			}
		}
		
		//Draws supports
		g.setColor(model.getState().supportColor);
		ListIterator sts = supports.listIterator();
		while(sts.hasNext()) {
			draw = (Draw)sts.next();
			no = (Node)draw.getRepresents();
			IPoint3d p3 = no.getCoord();
			draw.setLocation(da.getViewPoint( new Point2D.Double(p3.x, p3.y) ));
			draw.setScale(supportScale);
			draw.draw(g);
		}
		
		//Draws liberations
		g.setColor(model.getState().supportColor);
		ListIterator libs = liberations.listIterator();
		while(libs.hasNext()) {
			draw = (Draw)libs.next();
			draw.setScale(supportScale);
			draw.draw(g);
		}
		
		//Draws springs
		g.setColor(model.getState().springColor);
		ListIterator sps = springs.listIterator();
		while(sps.hasNext()) {
			draw = (Draw)sps.next();
			no = (Node)draw.getRepresents();
			IPoint3d p3 = no.getCoord();
			draw.setLocation(da.getViewPoint( new Point2D.Double(p3.x, p3.y) ));
			draw.setScale(supportScale);
			draw.draw(g);
		}
		
		//Draws Fx diagrams
		if (model.getState().viewFxDiagram) {
			g.setColor(model.getState().fxDiagramColor);
			ListIterator dias = fxDiagrams.listIterator();
			while(dias.hasNext()) {
				draw = (Draw)dias.next();
				draw.setScale(diagramScale * sFx);
				draw.draw(g);
			}
		}
		
		//Draws Fy diagrams
		if (model.getState().viewFyDiagram) {
			g.setColor(model.getState().fyDiagramColor);
			ListIterator dias = fyDiagrams.listIterator();
			while(dias.hasNext()) {
				draw = (Draw)dias.next();
				draw.setScale(diagramScale * sFy);
				draw.draw(g);
			}
		}
		
		//Draws Fz diagrams
		if (model.getState().viewFzDiagram) {
			g.setColor(model.getState().fzDiagramColor);
			ListIterator dias = fzDiagrams.listIterator();
			while(dias.hasNext()) {
				draw = (Draw)dias.next();
				draw.setScale(diagramScale * sFz);
				draw.draw(g);
			}
		}
		
		//Draws Mx diagrams
		if (model.getState().viewMxDiagram) {
			g.setColor(model.getState().mxDiagramColor);
			ListIterator dias = mxDiagrams.listIterator();
			while(dias.hasNext()) {
				draw = (Draw)dias.next();
				draw.setScale(diagramScale * sMx);
				draw.draw(g);
			}
		}
		
		//Draws My diagrams
		if (model.getState().viewMyDiagram) {
			g.setColor(model.getState().myDiagramColor);
			ListIterator dias = myDiagrams.listIterator();
			while(dias.hasNext()) {
				draw = (Draw)dias.next();
				draw.setScale(diagramScale * sMy);
				draw.draw(g);
			}
		}
		
		//Draws Mz diagrams
		if (model.getState().viewMzDiagram) {
			g.setColor(model.getState().mzDiagramColor);
			ListIterator dias = mzDiagrams.listIterator();
			while(dias.hasNext()) {
				draw = (Draw)dias.next();
				draw.setScale(diagramScale * sMz);
				draw.draw(g);
			}
		}
		
		//Draws Dx diagrams
		if (model.getState().viewDxDiagram) {
			g.setColor(model.getState().dxDiagramColor);
			ListIterator dias = dxDiagrams.listIterator();
			while(dias.hasNext()) {
				draw = (Draw)dias.next();
				draw.setScale(diagramScale * sDx);
				draw.draw(g);
			}
		}
		
		//Draws Dy diagrams
		if (model.getState().viewDyDiagram) {
			g.setColor(model.getState().dyDiagramColor);
			ListIterator dias = dyDiagrams.listIterator();
			while(dias.hasNext()) {
				draw = (Draw)dias.next();
				draw.setScale(-diagramScale * sDy);
				draw.draw(g);
			}
		}
		
		//Draws Dz diagrams
		if (model.getState().viewDzDiagram) {
			g.setColor(model.getState().dzDiagramColor);
			ListIterator dias = dzDiagrams.listIterator();
			while(dias.hasNext()) {
				draw = (Draw)dias.next();
				draw.setScale(diagramScale * sDz);
				draw.draw(g);
			}
		}
		
		//Draws Rx diagrams
		if (model.getState().viewRxDiagram) {
			g.setColor(model.getState().rxDiagramColor);
			ListIterator dias = rxDiagrams.listIterator();
			while(dias.hasNext()) {
				draw = (Draw)dias.next();
				draw.setScale(diagramScale * sRx);
				draw.draw(g);
			}
		}
		
		//Draws Ry diagrams
		if (model.getState().viewRyDiagram) {
			g.setColor(model.getState().ryDiagramColor);
			ListIterator dias = ryDiagrams.listIterator();
			while(dias.hasNext()) {
				draw = (Draw)dias.next();
				draw.setScale(diagramScale * sRy);
				draw.draw(g);
			}
		}
		
		//Draws Rz diagrams
		if (model.getState().viewRzDiagram) {
			g.setColor(model.getState().rzDiagramColor);
			ListIterator dias = rzDiagrams.listIterator();
			while(dias.hasNext()) {
				draw = (Draw)dias.next();
				draw.setScale(-diagramScale * sRz);
				draw.draw(g);
			}
		}
		
		//Draws nodes
		g.setColor(model.getState().pointColor);
		ListIterator pts = nodes.listIterator();
		while(pts.hasNext()) {
			draw = (Draw)pts.next();
			no = (Node)draw.getRepresents();
			if (model.getState().getViewNodeLabel())
				draw.setLabel(no.getLabel());
			else draw.setLabel("");
			if (model.getDiscreteModel().getGlobalAnalysisModel().getType().equals("PlaneTruss")){
				g.setColor(model.getState().pointColor);
				draw.setScale(nodeScale*2);
				draw.draw(g);
				Draw draw2 = (Draw)draw.clone();
				g.setColor(model.getState().getBackground());
				draw2.setScale(nodeScale*1);
				draw2.setSelected(false);
				draw2.setSelectable(false);
				draw2.draw(g);
			} else {
				draw.setScale(nodeScale*1);
				draw.draw(g);
			}
		}
		
	}
	
//*****************************************************************************
	
    public void fit() {
        
        //Calculating bounds
        if (nodes.size() >= 2){
            Point leftUp = new Point();
            Point rightDown = new Point();
            PointDraw po;
            ListIterator pts = nodes.listIterator();
            po = (PointDraw)pts.next();
            leftUp.x = po.getLocation().x;
            leftUp.y = po.getLocation().y;
            rightDown.x = po.getLocation().x;
            rightDown.y = po.getLocation().y;
            while (pts.hasNext()){
                po = (PointDraw)pts.next();
                if (po.getLocation().x < leftUp.x){
                    leftUp.x = po.getLocation().x;
                }
                if (po.getLocation().x > rightDown.x){
                    rightDown.x = po.getLocation().x;
                }
                if (po.getLocation().y < leftUp.y){
                    leftUp.y = po.getLocation().y;
                }
                if (po.getLocation().y > rightDown.y){
                    rightDown.y = po.getLocation().y;
                }
            }
			
            //Adding some border
            leftUp.x = leftUp.x - 15;
            leftUp.y = leftUp.y - 15;
            rightDown.x = rightDown.x + 15;
            rightDown.y = rightDown.y + 15;
            
            Point2D p1 = da.getWorldPoint(leftUp);
            Point2D p2 = da.getWorldPoint(rightDown);
            Rectangle2D r = new Rectangle2D.Double(p1.getX(), p2.getY(), p2.getX(), p1.getY());
            
			da.setZoom(r);
        }
    }
	
//*****************************************************************************
	
    public void add(Object obj) {
		
		if (obj instanceof SpringDraw) {
			SpringDraw sd = (SpringDraw)obj;
			Node no = (Node)sd.getRepresents();
			IPoint3d p3 = no.getCoord();
			sd.setLocation( da.getViewPoint(new Point2D.Double(p3.x, p3.y)) );
			springs.add(sd);
		}
		
		if (obj instanceof SupportDraw) {
			SupportDraw s = (SupportDraw)obj;
			Node no = (Node)s.getRepresents();
			if (no != null){
				IPoint3d p3 = no.getCoord();
				s.setLocation(da.getViewPoint( new Point2D.Double(p3.x, p3.y) ));
				supports.add(s);
			}
		}
		
	}
	
//*****************************************************************************
	
    public boolean contains(Point2D po) {
        Point p = da.getViewPoint(po);
        Draw draw;
        
        ListIterator pts = nodes.listIterator();
        while(pts.hasNext()) {
            draw = (Draw)pts.next();
            if (draw.contains(p)) {
                return true;
            }
        }
		
		ListIterator lns = elements.listIterator();
		while(lns.hasNext()) {
			draw = (Draw)lns.next();
			if (draw.contains(p)) {
				return true;
			}
		}
        return false;
    }
	
//*****************************************************************************
	
    public boolean contains(Line2D l) {
        Point  p1 = da.getViewPoint(l.getP1());
        Point  p2 = da.getViewPoint(l.getP2());
        Draw draw;
        
        ListIterator lns = elements.listIterator();
        while(lns.hasNext()) {
            draw = (Draw)lns.next();
            if (draw.contains(p1) && draw.contains(p2)) {
                return true;
            }
        }
        
        return false;
    }
	
//*****************************************************************************
	
    public Point pick(Point p) {
        
        Draw draw;
		
        ListIterator pts = nodes.listIterator();
        while(pts.hasNext()) {
            draw = (Draw)pts.next();
            if (draw.contains(p)) {
                return draw.getLocation();
            }
        }
		ListIterator lns = elements.listIterator();
		while(lns.hasNext()) {
			draw = (Draw)lns.next();
			if (draw.contains(p)) {
				return draw.getLocation();
			}
		}
        return p;
    }
	
//*****************************************************************************
	
    public void remove(Object obj) {
        /*
		if (obj instanceof Node) {
            fmodel.remove((Node)obj);
        }
        if (obj instanceof Element) {
            fmodel.remove((Element)obj);
        }
        if (obj instanceof Material) {
            fmodel.remove((Material)obj);
        }
        if (obj instanceof CrossSection) {
            fmodel.remove((CrossSection)obj);
        }
		if (obj instanceof AnalysisModel) {
            fmodel.remove((AnalysisModel)obj);
        }
		this.compose();
		*/
		
    }
	
//*****************************************************************************
	
    public List removeSelection() {
        
        ArrayList<Object> sel = new ArrayList<Object>();
		PointDraw pd;
        LineDraw ld;
        int index;
        
        ListIterator pts = nodes.listIterator();
        while(pts.hasNext()) {
            pd = (PointDraw)pts.next();
            if (pd.isSelected()) {
				Node p = (Node)pd.getRepresents();
				index = selection.indexOf(pd.getRepresents());
				selection.remove(index);
                fmodel.remove(p);
                sel.add(p);
            }
        }
		ListIterator lns = elements.listIterator();
		while(lns.hasNext()) {
			ld = (LineDraw)lns.next();
			if (ld.isSelected()){
				Element l = (Element)ld.getRepresents();
				index = selection.indexOf(ld.getRepresents());
				selection.remove(index);
				fmodel.remove(l);
				sel.add(l);
			}
		}
		this.compose();
		
		return sel;
		
    }
    
//*****************************************************************************
	
    public void removeAll() {
        nodes.clear();
		elements.clear();
		
		supports.clear();
		springs.clear();
		
		localAxis.clear();
		liberations.clear();
		elasticConnections.clear();
		
		fxDiagrams.clear();
		fyDiagrams.clear();
		fzDiagrams.clear();
		mxDiagrams.clear();
		myDiagrams.clear();
		mzDiagrams.clear();
		dxDiagrams.clear();
		dyDiagrams.clear();
		dzDiagrams.clear();
		rxDiagrams.clear();
		ryDiagrams.clear();
		rzDiagrams.clear();
    }
	
//*****************************************************************************
	
	public void unSelectPoints() {
        Draw draw;
        
        ListIterator pts = nodes.listIterator();
        while(pts.hasNext()) {
            draw = (Draw)pts.next();
            draw.setSelected(false);
        }
	}
	
//*****************************************************************************
	
    public ArrayList getSelection() {
        return selection;
    }
	
//*****************************************************************************
	
    public Object get(Point2D po) {
		Point location = da.getViewPoint(po);
        Draw draw;
        
        ListIterator pts = nodes.listIterator();
        while(pts.hasNext()) {
            draw = (Draw)pts.next();
            if (draw.contains(location)) {
                return draw.getRepresents();
            }
        }
		ListIterator lns = elements.listIterator();
		while(lns.hasNext()) {
			draw = (Draw)lns.next();
			if (draw.contains(location)){
				return draw.getRepresents();
			}
		}
		
        return null;
    }
    
//*****************************************************************************
	
    public boolean isSelected() {
        Draw draw;
        
        ListIterator pts = nodes.listIterator();
        while(pts.hasNext()) {
            draw = (Draw)pts.next();
            if (draw.isSelected()) {
                return true;
            }
        }
		ListIterator lns = elements.listIterator();
		while(lns.hasNext()) {
			draw = (Draw)lns.next();
			if (draw.isSelected()){
				return true;
			}
		}
		
		return false;
	}
	
//*****************************************************************************
	
	public void compose() {
		
		this.removeAll();
		
		//Filling the "nodes", "supports" and "springs" lists
		ListIterator femnodes = fmodel.getNodesList().listIterator();
		
		Node node;
		Point2D po;
		Point p;
		while (femnodes.hasNext()) {
			node = (Node)femnodes.next();
			po = new Point2D.Double(node.getCoord().x, node.getCoord().y);
			p = da.getViewPoint(po);
			nodes.add(new PointDraw(p, node));
			
			//adding supports and springs
			boolean Rdx = node.getRestraint(0);
			boolean Rdy = node.getRestraint(1);
			boolean Rrz = node.getRestraint(5);
			
			double Kx = node.getSpring(0);
			double Ky = node.getSpring(1);
			double Krz = node.getSpring(5);
			
			if (Rdx && !Rdy && !Rrz && Kx==0 && Ky==0 && Krz==0) {
				Draw draw = new SupportDraw.Y();
				draw.setRepresents(node);
				draw.setAngle(-Math.toRadians(90) + node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && Rdy && !Rrz && Kx==0 && Ky==0 && Krz==0) {
				Draw draw = new SupportDraw.Y();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && !Rdy && Rrz && Kx==0 && Ky==0 && Krz==0) {
				Draw draw = new SupportDraw.Z();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (Rdx && Rdy && !Rrz && Kx==0 && Ky==0 && Krz==0) {
				Draw draw = new SupportDraw.XY();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (Rdx && !Rdy && Rrz && Kx==0 && Ky==0 && Krz==0) {
				Draw draw = new SupportDraw.YZ();
				draw.setRepresents(node);
				draw.setAngle(-Math.toRadians(90) + node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && Rdy && Rrz && Kx==0 && Ky==0 && Krz==0) {
				Draw draw = new SupportDraw.YZ();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (Rdx && Rdy && Rrz && Kx==0 && Ky==0 && Krz==0) {
				Draw draw = new SupportDraw.XYZ();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && !Rdy && !Rrz && Kx!=0 && Ky==0 && Krz==0) {
				Draw draw = new SpringDraw.Dx();
				draw.setRepresents(node);
				draw.setAngle(Math.toRadians(90)*2 + node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && !Rdy && !Rrz && Kx==0 && Ky!=0 && Krz==0) {
				Draw draw = new SpringDraw.Dy();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && !Rdy && !Rrz && Kx==0 && Ky==0 && Krz!=0) {
				Draw draw = new SpringDraw.Rz();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && !Rdy && !Rrz && Kx!=0 && Ky!=0 && Krz==0) {
				Draw draw = new SpringDraw.Dx();
				draw.setRepresents(node);
				draw.setAngle(Math.toRadians(90)*2 + node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SpringDraw.Dy();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && !Rdy && !Rrz && Kx!=0 && Ky==0 && Krz!=0) {
				Draw draw = new SpringDraw.Dx();
				draw.setRepresents(node);
				draw.setAngle(Math.toRadians(90)*2 + node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SpringDraw.Rz();
				draw.setRepresents(node);
				draw.setAngle(-Math.toRadians(90) + node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && !Rdy && !Rrz && Kx==0 && Ky!=0 && Krz!=0) {
				Draw draw = new SpringDraw.Dy();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw); 
				draw = new SpringDraw.Rz();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && !Rdy && !Rrz && Kx!=0 && Ky!=0 && Krz!=0) {
				Draw draw = new SpringDraw.Dx();
				draw.setRepresents(node);
				draw.setAngle(Math.toRadians(90)*2 + node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SpringDraw.Dy();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SpringDraw.Rz();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (Rdx && !Rdy && !Rrz && Kx==0 && Ky!=0 && Krz==0) {
				Draw draw = new SupportDraw.Y();
				draw.setRepresents(node);
				draw.setAngle(-Math.toRadians(90) + node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SpringDraw.Dy();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (Rdx && !Rdy && !Rrz && Kx==0 && Ky==0 && Krz!=0) {
				Draw draw = new SupportDraw.Y();
				draw.setRepresents(node);
				draw.setAngle(-Math.toRadians(90) + node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SpringDraw.Rz();
				draw.setRepresents(node);
				draw.setAngle(-Math.toRadians(90) + node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && Rdy && !Rrz && Kx==0 && Ky==0 && Krz!=0) {
				Draw draw = new SupportDraw.Y();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SpringDraw.Rz();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && Rdy && !Rrz && Kx!=0 && Ky==0 && Krz==0) {
				Draw draw = new SpringDraw.Dx();
				draw.setRepresents(node);
				draw.setAngle(Math.toRadians(90)*2 + node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SupportDraw.Y();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && !Rdy && Rrz && Kx!=0 && Ky==0 && Krz==0) {
				Draw draw = new SpringDraw.Dx();
				draw.setRepresents(node);
				draw.setAngle(Math.toRadians(90)*2 + node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SupportDraw.Z();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && !Rdy && Rrz && Kx==0 && Ky!=0 && Krz==0) {
				Draw draw = new SpringDraw.Dy();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SupportDraw.Z();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (Rdx && Rdy && !Rrz && Kx==0 && Ky==0 && Krz!=0) {
				Draw draw = new SupportDraw.XY();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SpringDraw.Rz();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (Rdx && !Rdy && Rrz && Kx==0 && Ky!=0 && Krz==0) {
				Draw draw = new SpringDraw.Dy();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SupportDraw.YZ();
				draw.setRepresents(node);
				draw.setAngle(-Math.toRadians(90) + node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && Rdy && Rrz && Kx!=0 && Ky==0 && Krz==0) {
				Draw draw = new SpringDraw.Dx();
				draw.setRepresents(node);
				draw.setAngle(Math.toRadians(90)*2 + node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SupportDraw.YZ();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (Rdx && !Rdy && !Rrz && Kx==0 && Ky!=0 && Krz!=0) {
				Draw draw = new SupportDraw.Y();
				draw.setRepresents(node);
				draw.setAngle(-Math.toRadians(90) + node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SpringDraw.Dy();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SpringDraw.Rz();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && Rdy && !Rrz && Kx!=0 && Ky==0 && Krz!=0) {
				Draw draw = new SpringDraw.Dx();
				draw.setRepresents(node);
				draw.setAngle(Math.toRadians(90)*2 + node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SupportDraw.Y();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SpringDraw.Rz();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			} else if (!Rdx && !Rdy && Rrz && Kx!=0 && Ky!=0 && Krz==0) {
				Draw draw = new SpringDraw.Dx();
				draw.setRepresents(node);
				draw.setAngle(Math.toRadians(90)*2 + node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SpringDraw.Dy();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				draw = new SupportDraw.Z();
				draw.setRepresents(node);
				draw.setAngle(node.getAngle()*Math.PI/180);
				this.add(draw);
				
			}//end of adding supports
		
		}
		
		//Filling the "elements", "localAxis", "liberations", "elasticConnections",
		//"fxDiagrams", "fyDiagrams", "fzDiagrams", "mxDiagrams","myDiagrams",
		//"mzDiagrams", "dxDiagrams", "dyDiagrams", "dzDiagrams", "rxDiagrams",
		//"ryDiagrams", "rzDiagrams" lists
		ListIterator elms = fmodel.getElementsList().listIterator();
		FrameElement elm;
		Point p1;
		Point p2;
		
		double hFx = 0;
		double hFy = 0;
		double hFz = 0;
		double hMx = 0;
		double hMy = 0;
		double hMz = 0;
		
		double hDx = 0;
		double hDy = 0;
		double hDz = 0;
		double hRx = 0;
		double hRy = 0;
		double hRz = 0;
		
		while (elms.hasNext()) {
			elm = (FrameElement)elms.next();
			node = elm.getInitialNode();
			po = new Point2D.Double(node.getCoord().x, node.getCoord().y);
			p1 = da.getViewPoint(po);
			node = elm.getFinalNode();
			po = new Point2D.Double(node.getCoord().x, node.getCoord().y);
			p2 = da.getViewPoint(po);
			elements.add(new LineDraw(p1, p2, elm));
			
			//Setting the variables in (initial node) and fn (final node)
			Node in = elm.getInitialNode();
			Node fn = elm.getFinalNode();
			
			//Calculating the angle between local axis and global axis and its sine and cosine
			double elmAngle = 0;
			Point2D in2d = new Point2D.Double(in.getCoord().x, in.getCoord().y);
			Point2D fn2d = new Point2D.Double(fn.getCoord().x, fn.getCoord().y);
			in2d = da.getViewPoint(in2d);
			fn2d = da.getViewPoint(fn2d);
			
			if (fn.getCoord().x > in.getCoord().x) {
				elmAngle = -Math.atan((fn2d.getY() - in2d.getY()) / (fn2d.getX() - in2d.getX()));
			} else if (fn.getCoord().x < in.getCoord().x) {
				elmAngle = -Math.atan((fn2d.getY() - in2d.getY()) / (fn2d.getX() - in2d.getX())) + Math.toRadians(180);
			} else if ((fn.getCoord().x==in.getCoord().x) && (fn.getCoord().y>in.getCoord().y)) {
				elmAngle = Math.toRadians(90);
			} else if ((fn.getCoord().x==in.getCoord().x) && (fn.getCoord().y<in.getCoord().y)) {
				elmAngle = Math.toRadians(-90);
			}
			
			//Adding Local Axis
			Draw draw2 = new AxisDraw();
			draw2.setRepresents(elm);
			draw2.setAngle(elmAngle);
			draw2.setLocation(p1);
			((AxisDraw)draw2).setLabel1("X");
			((AxisDraw)draw2).setLabel2("Y");
			localAxis.add(draw2);
			
			//Adding liberations
			//Dx1 liberation
			if (elm.getLiberation(0) && !elm.getLiberation(1) && !elm.getLiberation(5)){
				Draw draw = new LiberationDraw.X();
				draw.setRepresents(elm);
				draw.setAngle(elmAngle);
				draw.setLocation(p1);
				((LiberationDraw)draw).setModelState(model.getState());
				liberations.add(draw);
			}
			//Dy1 liberation
			if (!elm.getLiberation(0) && elm.getLiberation(1) && !elm.getLiberation(5)){
				Draw draw = new LiberationDraw.Y();
				draw.setRepresents(elm);
				draw.setAngle(elmAngle);
				draw.setLocation(p1);
				((LiberationDraw)draw).setModelState(model.getState());
				liberations.add(draw);
			}
			//Rz1 liberation
			if (!elm.getLiberation(0) && !elm.getLiberation(1) && elm.getLiberation(5)){
				Draw draw = new LiberationDraw.Z();
				draw.setRepresents(elm);
				draw.setAngle(elmAngle);
				draw.setLocation(p1);
				((LiberationDraw)draw).setModelState(model.getState());
				liberations.add(draw);
			}
			//Dx1 and Dy1 liberation
			if (elm.getLiberation(0) && elm.getLiberation(1) && !elm.getLiberation(5)){
				Draw draw = new LiberationDraw.XY();
				draw.setRepresents(elm);
				draw.setAngle(elmAngle);
				draw.setLocation(p1);
				((LiberationDraw)draw).setModelState(model.getState());
				liberations.add(draw);
			}
			//Dx1 and Rz1 liberation
			if (elm.getLiberation(0) && !elm.getLiberation(1) && elm.getLiberation(5)){
				Draw draw = new LiberationDraw.XZ();
				draw.setRepresents(elm);
				draw.setAngle(elmAngle);
				draw.setLocation(p1);
				((LiberationDraw)draw).setModelState(model.getState());
				liberations.add(draw);
			}
			//Dy1 and Rz1 liberation
			if (!elm.getLiberation(0) && elm.getLiberation(1) && elm.getLiberation(5)){
				Draw draw = new LiberationDraw.YZ();
				draw.setRepresents(elm);
				draw.setAngle(elmAngle);
				draw.setLocation(p1);
				((LiberationDraw)draw).setModelState(model.getState());
				liberations.add(draw);
			}
			//Dx1, Dy1 and Rz1 liberation
			if (elm.getLiberation(0) && elm.getLiberation(1) && elm.getLiberation(5)){
				Draw draw = new LiberationDraw.XYZ();
				draw.setRepresents(elm);
				draw.setAngle(elmAngle);
				draw.setLocation(p1);
				((LiberationDraw)draw).setModelState(model.getState());
				liberations.add(draw);
			}
			
			//Dx2 liberation
			if (elm.getLiberation(6) && !elm.getLiberation(7) && !elm.getLiberation(11)){
				Draw draw = new LiberationDraw.X();
				draw.setRepresents(elm);
				draw.setAngle(elmAngle + Math.toRadians(180));
				draw.setLocation(p2);
				((LiberationDraw)draw).setModelState(model.getState());
				liberations.add(draw);
			}
			//Dy2 liberation
			if (!elm.getLiberation(6) && elm.getLiberation(7) && !elm.getLiberation(11)){
				Draw draw = new LiberationDraw.Y();
				draw.setRepresents(elm);
				draw.setAngle(elmAngle + Math.toRadians(180));
				draw.setLocation(p2);
				((LiberationDraw)draw).setModelState(model.getState());
				liberations.add(draw);
			}
			//Rz2 liberation
			if (!elm.getLiberation(6) && !elm.getLiberation(7) && elm.getLiberation(11)){
				Draw draw = new LiberationDraw.Z();
				draw.setRepresents(elm);
				draw.setAngle(elmAngle + Math.toRadians(180));
				draw.setLocation(p2);
				((LiberationDraw)draw).setModelState(model.getState());
				liberations.add(draw);
			}
			//Dx2 and Dy2 liberation
			if (elm.getLiberation(6) && elm.getLiberation(7) && !elm.getLiberation(11)){
				Draw draw = new LiberationDraw.XY();
				draw.setRepresents(elm);
				draw.setAngle(elmAngle + Math.toRadians(180));
				draw.setLocation(p2);
				((LiberationDraw)draw).setModelState(model.getState());
				liberations.add(draw);
			}
			//Dx2 and Rz2 liberation
			if (elm.getLiberation(6) && !elm.getLiberation(7) && elm.getLiberation(11)){
				Draw draw = new LiberationDraw.XZ();
				draw.setRepresents(elm);
				draw.setAngle(elmAngle + Math.toRadians(180));
				draw.setLocation(p2);
				((LiberationDraw)draw).setModelState(model.getState());
				liberations.add(draw);
			}
			//Dy2 and Rz2 liberation
			if (!elm.getLiberation(6) && elm.getLiberation(7) && elm.getLiberation(11)){
				Draw draw = new LiberationDraw.YZ();
				draw.setRepresents(elm);
				draw.setAngle(elmAngle + Math.toRadians(180));
				draw.setLocation(p2);
				((LiberationDraw)draw).setModelState(model.getState());
				liberations.add(draw);
			}
			//Dx2, Dy2 and Rz2 liberation
			if (elm.getLiberation(6) && elm.getLiberation(7) && elm.getLiberation(11)){
				Draw draw = new LiberationDraw.XYZ();
				draw.setRepresents(elm);
				draw.setAngle(elmAngle + Math.toRadians(180));
				draw.setLocation(p2);
				((LiberationDraw)draw).setModelState(model.getState());
				liberations.add(draw);
			}
			//End of adding liberations
			
			//Adding elastic connections
			if (elm.getMaterial() instanceof MSpring) {
				//MSpring mat = (MSpring)elm.getMaterial();
			}
			//End of adding elastic connections
			
			//Creating the Effort object for this element
			PointForce ipf = new PointForce();
			ArrayList cfs = new ArrayList();
			ArrayList efs = new ArrayList();
			Effort ef;
			
			//Actions at elements extremity (initial node)
			ipf.setCoord(0,0,0);
			ipf.setForce(0, elm.getActionAtExtremity(0));
			ipf.setForce(1, elm.getActionAtExtremity(1));
			ipf.setForce(2, elm.getActionAtExtremity(2));
			ipf.setForce(3, elm.getActionAtExtremity(3));
			ipf.setForce(4, elm.getActionAtExtremity(4));
			ipf.setForce(5, elm.getActionAtExtremity(5));
			
			//Displacements at elements extremity (initial node)
			double[] tempIniDisp = new double[6];
			tempIniDisp[0] = elm.getInitialNode().getDisplacement(0);
			tempIniDisp[1] = elm.getInitialNode().getDisplacement(1);
			tempIniDisp[2] = elm.getInitialNode().getDisplacement(2);
			tempIniDisp[3] = elm.getInitialNode().getDisplacement(3);
			tempIniDisp[4] = elm.getInitialNode().getDisplacement(4);
			tempIniDisp[5] = elm.getInitialNode().getDisplacement(5);
			
			//Calculating element displacement when liberation exists
			//Just for the initial node
			FrameElmAnalysis anl = (FrameElmAnalysis)elm.getAnalysisModel();
			int eq = -1;
			for (int i=0; i<6; i++) {
				if (anl.getValidEquation(i))
					eq++;
				if (elm.getLiberation(i) && anl.getValidEquation(i)) {
					IMatrix k = anl.localStiffnessMatrix(elm);
					double fei = anl.localEquivalentForceVector(elm).getElement(eq);
					double kii = k.getElement(eq,eq);
					double di = 0;
					double sum = 0;
					for (int j=0; j<anl.getNdf(); j++) {
						if (eq!=j) {
							double dj = elm.getInitialNode().getDisplacement(j);
							double kij = k.getElement(eq,j);
							sum += kij * dj;
						}
					}
					di = (fei - sum) / kii;
					tempIniDisp[i] += di;
				}
			}
			
			//It must be corrected for 3D space!!!
			double nodeAngle = elm.getInitialNode().getAngle();
			double sin2 = Math.sin(Math.toRadians(nodeAngle-elmAngle));
			double cos2 = Math.cos(Math.toRadians(nodeAngle-elmAngle));
			
			double[] iniDisp = new double[6];
			iniDisp[0] = tempIniDisp[0] * cos2 - tempIniDisp[1] * sin2;
			iniDisp[1] = tempIniDisp[0] * sin2 + tempIniDisp[1] * cos2;
			iniDisp[2] = tempIniDisp[3];
			iniDisp[3] = tempIniDisp[3];
			iniDisp[4] = tempIniDisp[4];
			iniDisp[5] = tempIniDisp[5];
			
			cfs = elm.getElmPointForcesList();
			efs = elm.getElmLineForcesList();
			
			ef = new Effort(ipf, cfs, efs);
			
			double viewLength = in2d.distance(fn2d);
			double elmLength = elm.getLength();
			double incX = elmLength / viewLength;
			int n = (int)viewLength + 1;
			
			double e = ((LinearElasticIsotropic)elm.getMaterial()).getMaterialValues("ELASTICITY").doubleValue();
			double g = ((LinearElasticIsotropic)elm.getMaterial()).getMaterialValues("SHEAR_MODULUS").doubleValue();
			double area = elm.getCrossSection().getArea();
			double ix = elm.getCrossSection().getIx();
			double iy = elm.getCrossSection().getIy();
			double iz = elm.getCrossSection().getIz();
			
			double[] x = new double[n];
			double[] x2 = new double[n];
			
			double[] fx = new double[n];
			double[] fy = new double[n];
			double[] fz = new double[n];
			double[] mx = new double[n];
			double[] my = new double[n];
			double[] mz = new double[n];
			
			double[] dx = new double[n];
			double[] dy = new double[n];
			double[] dz = new double[n];
			double[] rx = new double[n];
			double[] ry = new double[n];
			double[] rz = new double[n];
			
			for (int i=0; i<n; i++) {
				x[i] = i*incX;
				x2[i] = x[i] / incX;
				
				ef.setSection(x[i]);
				fx[i] = -ef.getActionFXAtSection();
				fy[i] = ef.getActionFYAtSection();
				fz[i] = ef.getActionFZAtSection();
				mx[i] = ef.getActionMXAtSection();
				my[i] = ef.getActionMYAtSection();
				mz[i] = ef.getActionMZAtSection();
				
				//calculating rotation at X axis
				if (i == 0) {
					rx[0] = iniDisp[3];
				} else {
					double deltaX = x[i] - x[i-1];
					double a = (mx[i] + mx[i-1]) /2 * deltaX;
					rx[i] = rx[i-1] + a / (g * ix);
				}
				
				//calculating rotation at Y axis
				if (i == 0) {
					ry[0] = iniDisp[4];
				} else {
					double deltaX = x[i] - x[i-1];
					double a = (my[i] + my[i-1]) /2 * deltaX;
					ry[i] = ry[i-1] + a / (e * iy);
				}
				
				//calculating rotation at Z axis
				if (i == 0) {
					rz[0] = iniDisp[5];
				} else {
					double deltaX = x[i] - x[i-1];
					double a = (mz[i] + mz[i-1]) /2 * deltaX;
					rz[i] = rz[i-1] + a / (e * iz);
					
					//Contribution of temperature change
					if ((elm.getDeltaTemp(0)!=0 || elm.getDeltaTemp(2)!=0) && elm.getCrossSection().getHeight()!=0) {
						
						double alpha = ((LinearElasticIsotropic)elm.getMaterial()).getMaterialValues("THERMAL_COEFF").doubleValue();
						double h = elm.getCrossSection().getHeight();
						double ts = elm.getDeltaTemp(0);
						double ti = elm.getDeltaTemp(2);
						double c = -alpha * (ti - ts) / h;
						double r = c * deltaX;
						rz[i] -= r;
					}
					
				}
				
				//calculating translation at X axis
				if (i == 0) {
					dx[0] = iniDisp[0];
				} else {
					double deltaX = x[i] - x[i-1];
					double a = (fx[i] + fx[i-1]) /2 * deltaX;
					dx[i] = dx[i-1] + a / (e * area);
					
					//Contribution of temperature change
					if (elm.getDeltaTemp(1)!=0) {
						double alpha = ((LinearElasticIsotropic)elm.getMaterial()).getMaterialValues("THERMAL_COEFF").doubleValue();
						double tn = elm.getDeltaTemp(1);
						double c = alpha * tn * deltaX;
						dx[i] -= c;
					}
				}
				
				//calculating translation at Y axis
				if (i == 0) {
					dy[0] = iniDisp[1];
				} else {
					double deltaX = x[i] - x[i-1];
					double q = Math.pow(deltaX, 2) / 6 * (2 * mz[i-1] + mz[i]);
					dy[i] = dy[i-1] + rz[i-1] * deltaX + q / (e*iz);
					
					//Contribution of temperature change
					if ((elm.getDeltaTemp(0)!=0 || elm.getDeltaTemp(2)!=0) && elm.getCrossSection().getHeight()!=0) {
						double alpha = ((LinearElasticIsotropic)elm.getMaterial()).getMaterialValues("THERMAL_COEFF").doubleValue();
						double h = elm.getCrossSection().getHeight();
						double ts = elm.getDeltaTemp(0);
						double ti = elm.getDeltaTemp(2);
						double c = -alpha * (ti - ts) / h;
						double v = c * Math.pow(deltaX, 2) / 2;
						dy[i] -= v;
					}
				}
				
				//calculating translation at Z axis
				if (i == 0) {
					dz[0] = iniDisp[2];
				} else {
					double deltaX = x[i] - x[i-1];
					double q = Math.pow(deltaX, 2) / 6 * (2 * my[i-1] + my[i]);
					dz[i] = dz[i-1] + ry[i-1] * deltaX + q / (e*iy);
				}
				
				if (Math.abs(fx[i]) > hFx)
					hFx = Math.abs(fx[i]);
				if (Math.abs(fy[i]) > hFy)
					hFy = Math.abs(fy[i]);
				if (Math.abs(fz[i]) > hFz)
					hFz = Math.abs(fz[i]);
				if (Math.abs(mx[i]) > hMx)
					hMx = Math.abs(mx[i]);
				if (Math.abs(my[i]) > hMy)
					hMy = Math.abs(my[i]);
				if (Math.abs(mz[i]) > hMz)
					hMz = Math.abs(mz[i]);
				
				if (Math.abs(dx[i]) > hDx)
					hDx = Math.abs(dx[i]);
				if (Math.abs(dy[i]) > hDy)
					hDy = Math.abs(dy[i]);
				if (Math.abs(dz[i]) > hDz)
					hDz = Math.abs(dz[i]);
				if (Math.abs(rx[i]) > hRx)
					hRx = Math.abs(rx[i]);
				if (Math.abs(ry[i]) > hRy)
					hRy = Math.abs(ry[i]);
				if (Math.abs(rz[i]) > hRz)
					hRz = Math.abs(rz[i]);
				
			}
			
			if (hDy==0) {
				for (int i=0; i<n; i++) {
					if (Math.abs(dx[i]) > hDy)
						hDy = Math.abs(dx[i]);
				}
			}
			
			//Adding fxDiagrams
			DiagramDraw diaDraw = new DiagramDraw(n, x, fx);
			diaDraw.setAngle(elmAngle);
			diaDraw.setLocation(p1);
			diaDraw.setFactor(incX);
			fxDiagrams.add(diaDraw);
			//End of adding fxDiagrams
			
			//Adding fyDiagrams
			diaDraw = new DiagramDraw(n, x, fy);
			diaDraw.setAngle(elmAngle);
			diaDraw.setLocation(p1);
			diaDraw.setFactor(incX);
			fyDiagrams.add(diaDraw);
			//End of adding fyDiagrams
			
			//Adding fzDiagrams
			diaDraw = new DiagramDraw(n, x, fz);
			diaDraw.setAngle(elmAngle);
			diaDraw.setLocation(p1);
			diaDraw.setFactor(incX);
			fzDiagrams.add(diaDraw);
			//End of adding fzDiagrams
			
			//Adding mxDiagrams
			diaDraw = new DiagramDraw(n, x, mx);
			diaDraw.setAngle(elmAngle);
			diaDraw.setLocation(p1);
			diaDraw.setFactor(incX);
			mxDiagrams.add(diaDraw);
			//End of adding mxDiagrams
			
			//Adding myDiagrams
			diaDraw = new DiagramDraw(n, x, my);
			diaDraw.setAngle(elmAngle);
			diaDraw.setLocation(p1);
			diaDraw.setFactor(incX);
			myDiagrams.add(diaDraw);
			//End of adding myDiagrams
			
			//Adding mzDiagrams
			diaDraw = new DiagramDraw(n, x, mz);
			diaDraw.setAngle(elmAngle);
			diaDraw.setLocation(p1);
			diaDraw.setFactor(incX);
			mzDiagrams.add(diaDraw);
			//End of adding mzDiagrams
			
			//Adding dxDiagrams
			diaDraw = new DiagramDraw(n, x, dx);
			diaDraw.setAngle(elmAngle);
			diaDraw.setLocation(p1);
			diaDraw.setFactor(incX);
			dxDiagrams.add(diaDraw);
			//End of adding dxDiagrams
			
			//Adding dyDiagrams
			diaDraw = new DiagramDraw(n, x, dy);
			diaDraw.setAngle(elmAngle);
			diaDraw.setLocation(p1);
			diaDraw.setFactor(incX);
			dyDiagrams.add(diaDraw);
			//End of adding dyDiagrams
			
			//Adding dzDiagrams
			diaDraw = new DiagramDraw(n, x, dz);
			diaDraw.setAngle(elmAngle);
			diaDraw.setLocation(p1);
			diaDraw.setFactor(incX);
			dzDiagrams.add(diaDraw);
			//End of adding dzDiagrams
			
			//Adding rxDiagrams
			diaDraw = new DiagramDraw(n, x, rx);
			diaDraw.setAngle(elmAngle);
			diaDraw.setLocation(p1);
			diaDraw.setFactor(incX);
			rxDiagrams.add(diaDraw);
			//End of adding dxDiagrams
			
			//Adding ryDiagrams
			diaDraw = new DiagramDraw(n, x, ry);
			diaDraw.setAngle(elmAngle);
			diaDraw.setLocation(p1);
			diaDraw.setFactor(incX);
			ryDiagrams.add(diaDraw);
			//End of adding ryDiagrams
			
			//Adding rzDiagrams
			diaDraw = new DiagramDraw(n, x, rz);
			diaDraw.setAngle(elmAngle);
			diaDraw.setLocation(p1);
			diaDraw.setFactor(incX);
			rzDiagrams.add(diaDraw);
			//End of adding rzDiagrams
			
		}//end of elements
		
		//Setting scales
		if (hFx != 0 && hFx > 1e-10)
			sFx = 125/hFx;
		if (hFy != 0 && hFy > 1e-10)
			sFy = 150/hFy;
		if (hFz != 0 && hFz > 1e-10)
			sFz = 150/hFz;
		if (hMx != 0 && hMx > 1e-10)
			sMx = 125/hMx;
		if (hMy != 0 && hMy > 1e-10)
			sMy = 200/hMy;
		if (hMz != 0 && hMz > 1e-10)
			sMz = 200/hMz;
		
		if (hDx != 0 && hDx > 1e-10)
			sDx = 100/hDx;
		if (hDy != 0 && hDy > 1e-10)
			sDy = 100/hDy;
		if (hDz != 0 && hDz > 1e-10)
			sDz = 100/hDz;
		if (hRx != 0 && hRx > 1e-10)
			sRx = 100/hRx;
		if (hRy != 0 && hRy > 1e-10)
			sRy = 100/hRy;
		if (hRz != 0 && hRz > 1e-10)
			sRz = 100/hRz;
	}
	
//*****************************************************************************
	
	public void unSelectAll() {
		selection.clear();
        /*
        ListIterator pts = elements.listIterator();
        while(pts.hasNext()) {
            draw = (Draw)pts.next();
            draw.setSelected(false);
        }*/
		
	}
	
//*****************************************************************************
	
	public void selectAll() {
		selection.clear();
        /*
        ListIterator pts = elements.listIterator();
        while(pts.hasNext()) {
            draw = (Draw)pts.next();
            draw.setSelected(false);
        }*/
		
	}
	
//*****************************************************************************
	
	public void select(Point p) {
		/*
		ListIterator lns = elements.listIterator();
		while(lns.hasNext()) {
			draw = (Draw)lns.next();
			if (draw.contains(p)){
				if (draw.isSelected()) {
					draw.setSelected(false);
					index = selection.indexOf(draw.getRepresents());
					selection.remove(index);
					return;
				}else{
					draw.setSelected(true);
					selection.add(draw.getRepresents());
					return;
				}
			}
		}*/
		
	}

	public boolean contains(IPoint3d po) {
		// TODO Auto-generated method stub
		return false;
	}
	
//*****************************************************************************
}
